﻿Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class TilePicker
    Inherits UserControl

    Public Shared ReadOnly SelectedIndexProperty As DependencyProperty = DependencyProperty.Register("SelectedIndex",
                                                                                                     GetType(Integer),
                                                                                                     GetType(TilePicker),
                                                                                                     New FrameworkPropertyMetadata(AddressOf OnSelectedIndexChanged))
    Public Shared ReadOnly RowsProperty As DependencyProperty = DependencyProperty.Register("Rows",
                                                                                            GetType(Integer),
                                                                                            GetType(TilePicker),
                                                                                            New PropertyMetadata(16))
    Public Shared ReadOnly ColumnsProperty As DependencyProperty = DependencyProperty.Register("Columns",
                                                                                               GetType(Integer),
                                                                                               GetType(TilePicker),
                                                                                               New PropertyMetadata(16))
    Public Shared ReadOnly ImageProperty As DependencyProperty = DependencyProperty.Register("Image",
                                                                                             GetType(BitmapImage),
                                                                                             GetType(TilePicker))
    Public Shared ReadOnly TilesProperty As DependencyProperty = DependencyProperty.Register("Tiles",
                                                                                             GetType(ObservableCollection(Of TileData)),
                                                                                             GetType(TilePicker))
    Public Shared ReadOnly SelectionWidthProperty As DependencyProperty = DependencyProperty.Register("SelectionWidth",
                                                                                                      GetType(Double),
                                                                                                      GetType(TilePicker))
    Public Shared ReadOnly SelectionHeightProperty As DependencyProperty = DependencyProperty.Register("SelectionHeight",
                                                                                                       GetType(Double),
                                                                                                       GetType(TilePicker))
    Public Shared ReadOnly SelectionXProperty As DependencyProperty = DependencyProperty.Register("SelectionX",
                                                                                                  GetType(Double),
                                                                                                  GetType(TilePicker))
    Public Shared ReadOnly SelectionYProperty As DependencyProperty = DependencyProperty.Register("SelectionY",
                                                                                                  GetType(Double),
                                                                                                  GetType(TilePicker))

    Private Shared Sub OnSelectedIndexChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
        Dim editor As TilePicker = CType(sender, TilePicker)
        editor.Tiles(CInt(e.OldValue)).IsSelected = False
        editor.Tiles(CInt(e.NewValue)).IsSelected = True
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        If Not DesignerProperties.GetIsInDesignMode(New DependencyObject) Then

            Dim converter As New BitmapConverter
            Image = CType(converter.Convert(AppData.Config.TileSet.Image, GetType(BitmapImage), Nothing, System.Threading.Thread.CurrentThread.CurrentCulture), BitmapImage)
            Dim tiles As New ObservableCollection(Of TileData)
            Dim cols As Integer = Columns
            For y As Integer = 0 To Rows - 1
                For x As Integer = 0 To cols - 1
                    tiles.Add(New TileData With {.Index = y * cols + x})
                Next
            Next
            Me.Tiles = tiles
        End If
    End Sub


    Public Property SelectedIndex As Integer
        Get
            Return CInt(MyBase.GetValue(SelectedIndexProperty))
        End Get
        Set(value As Integer)
            MyBase.SetValue(SelectedIndexProperty, value)
        End Set
    End Property

    Public Property Image As BitmapImage
        Get
            Return CType(MyBase.GetValue(ImageProperty), BitmapImage)
        End Get
        Set(value As BitmapImage)
            MyBase.SetValue(ImageProperty, value)
        End Set
    End Property


    Private Sub TileRectangle_MouseDown(sender As Object, e As MouseButtonEventArgs)
        If sender IsNot Nothing Then
            Dim r As Rectangle = CType(sender, Rectangle)
            If r.DataContext IsNot Nothing Then
                Dim tileData As TileData = CType(r.DataContext, TileData)

                Me.SelectedIndex = tileData.Index
            End If
        End If
    End Sub

    Public Property Rows As Integer
        Get
            Return CInt(MyBase.GetValue(RowsProperty))
        End Get
        Set(value As Integer)
            MyBase.SetValue(RowsProperty, value)
        End Set
    End Property

    Public Property Columns As Integer
        Get
            Return CInt(MyBase.GetValue(ColumnsProperty))
        End Get
        Set(value As Integer)
            MyBase.SetValue(ColumnsProperty, value)
        End Set
    End Property

    Public Property Tiles As ObservableCollection(Of TileData)
        Get
            Return CType(MyBase.GetValue(TilesProperty), ObservableCollection(Of TileData))
        End Get
        Set(value As ObservableCollection(Of TileData))
            MyBase.SetValue(TilesProperty, value)
        End Set
    End Property


    Public ReadOnly Property SelectionWidth As Double
        Get
            Return CDbl(GetValue(SelectionWidthProperty))
        End Get
    End Property

    Public ReadOnly Property SelectionHeight As Double
        Get
            Return CDbl(GetValue(SelectionHeightProperty))
        End Get
    End Property


    Public ReadOnly Property SelectionX As Double
        Get
            Return CDbl(GetValue(SelectionXProperty))
        End Get
    End Property

    Public ReadOnly Property SelectionY As Double
        Get
            Return CDbl(GetValue(SelectionYProperty))
        End Get
    End Property

    Public Class TileData
        Inherits ViewModelBase

        Public Property Index As Integer

        Private mIsSelected As Boolean = False
        Public Property IsSelected As Boolean
            Get
                Return mIsSelected
            End Get
            Set(value As Boolean)
                If mIsSelected <> value Then
                    mIsSelected = value
                    RaisePropertyChanged("IsSelected")
                End If
            End Set
        End Property
    End Class



End Class
